package com.admission.service;

import java.io.File;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.admission.dao.ArtFormDao;
import com.admission.model.ArrayInfo;
import com.admission.model.ArtForm;
import com.admission.util.DeleteFile;

@Repository
@Transactional
public class ArtFormManager {
	private ArtFormDao artFormDao;
	private ArtDetailManager artDetailManager;
	private ArrayInfoManager arrayInfoManager;
	
	/**
	 * 通过id查找位置对象，无返回null
	 * 
	 * @param id
	 * @return
	 */
	public ArtForm getById(String id) {
		return artFormDao.findUniqueBy("id", id);
	}
	
	public void save(ArtForm entity,String sub){
		String serverrealPath = ServletActionContext.getServletContext()
				.getRealPath("/upload");
		if (sub != null) {
			File file = new File(serverrealPath + File.separator
					+ entity.getUrl());
			DeleteFile.deleteByFile(file);// 删除源文件	
			entity.setUrl(sub);
		}
		artFormDao.save(entity);
	}
	
	/**
	 * 通过filter分页查询
	 * 
	 * @param filters
	 * @return
	 */
	public Page<ArtForm> getPage(Page<ArtForm> page,List<PropertyFilter> filters) {
		return artFormDao.findPage(page,filters);
	}

	
	/**
	 * 批量删除
	 * 逗号隔开id集合
	 * @param ids
	 */
	public void deleteByIds(String ids){
		String serverrealPath = ServletActionContext.getServletContext()
				.getRealPath("/upload");
		for(String id:ids.split(",")){
			ArtForm entity=this.getById(id.trim());
			File file = new File(serverrealPath + File.separator
						+ entity.getUrl());
			DeleteFile.deleteByFile(file);// 删除源文件
			artDetailManager.deleteByIds(id.trim());
			artFormDao.delete(id.trim());
		}
	}
	
	/**
	 * 改变状态
	 * @param id
	 * @param status
	 */
	public void changeConsult(String status){
		arrayInfoManager.changeConsult(status, ArrayInfo.ARTCONSULT);
	}
	
	/**
	 *判断权限 
	 * @return
	 */
	public String checkConsult(){
		return arrayInfoManager.checkConsult(ArrayInfo.ARTCONSULT);
	}
	
	
	public ArtFormDao getArtFormDao() {
		return artFormDao;
	}
	@Autowired
	public void setArtFormDao(ArtFormDao artFormDao) {
		this.artFormDao = artFormDao;
	}

	public ArtDetailManager getArtDetailManager() {
		return artDetailManager;
	}
	@Autowired
	public void setArtDetailManager(ArtDetailManager artDetailManager) {
		this.artDetailManager = artDetailManager;
	}

	public ArrayInfoManager getArrayInfoManager() {
		return arrayInfoManager;
	}
	@Autowired
	public void setArrayInfoManager(ArrayInfoManager arrayInfoManager) {
		this.arrayInfoManager = arrayInfoManager;
	}
}
